/************************************************************************
Arise Engine		IniFile.cpp

					E-mail:	arise_engine@mail.ru

					Origin by Haaf's Game Engine 1.5:
					Copyright (C) 2003-2004, Relish Games, hge.relishgames.com

created:			04.02.2007
last changes:		04.02.2007
************************************************************************/
#include "stdafx.h"

IniFile::IniFile()
{
}
IniFile::IniFile(char* inifile_name)
{
	if(!inifile_name) return;
	char path[INIFILE_MAX_STR_PATH];
	GetCurrentDirectory(INIFILE_MAX_STR_PATH, path);
    sprintf(m_file_name,"%s\\%s", path, inifile_name);
}
IniFile::~IniFile()
{
}
		
void IniFile::SetInt(const char* section, const char* name, const int value) const
{
	char buf[INIFILE_BUF_MAX_CHAR];

	if(m_file_name[0])
	{
		sprintf(buf, "%d", value);
		WritePrivateProfileString(section, name, buf, m_file_name);
	}
}


int IniFile::GetInt(const char* section, const char* name, const int default_value) const
{
	char buf[INIFILE_BUF_MAX_CHAR];

	if(m_file_name[0]) 
	{
		if(GetPrivateProfileString(section, name, "", buf, sizeof(buf), m_file_name))
		{ 
			return atoi(buf); 
		}
		else 
		{ 
			return default_value; 
		}
	}
	return default_value;
}


void IniFile::SetFloat(const char* section, const char* name, const float value) const
{
	char buf[INIFILE_BUF_MAX_CHAR];

	if(m_file_name[0]) 
	{
		sprintf(buf, "%f", value);
		WritePrivateProfileString(section, name, buf, m_file_name);
	}
}


float IniFile::GetFloat(const char* section, const char* name, const float default_value) const
{
	char buf[INIFILE_BUF_MAX_CHAR];

	if(m_file_name[0]) 
	{
		if(GetPrivateProfileString(section, name, "", buf, sizeof(buf), m_file_name))
		{ 
			return (float)atof(buf); 
		}
		else 
		{ 
			return default_value; 
		}
	}
	return default_value;
}


void IniFile::SetString(const char* section, const char* name, const char* value) const
{
	if(m_file_name[0]) WritePrivateProfileString(section, name, value, m_file_name);
}


std::string IniFile::GetString(const char* section, const char* name, const char* default_value) const
{
	char buf_string[INIFILE_BUF_MAX_CHAR];

	if(m_file_name[0])
	{
		GetPrivateProfileString(	section, 
									name, 
									default_value, 
									buf_string, 
									sizeof(buf_string), 
									m_file_name	);
	}
	else strcpy(buf_string, default_value);

	std::string s = buf_string;
	return s;
}

